﻿
using System;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using System.Threading.Tasks;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Surging.Core.Common;
using Dora.Services.AdminUser.Dto.QueryDto;
using System.Collections.Generic;

namespace Dora.Services.AdminUser.Repository
{

    /// <summary>
    /// 大社产品类型/目的地保险产品默认排除供应商不绑定保险仓储层
    /// </summary>
    public class OrgInsuranceSupplierRepository : SqlSugarRepository<OrgInsuranceSupplierEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public OrgInsuranceSupplierRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        public async Task<ResultDto<List<OrgInsuranceSupplierEntity>>> BatchInsertOrgInsuranceExcludes(List<OrgInsuranceSupplierEntity> OrgInsuranceExcludes)
        {
            List<OrgInsuranceSupplierEntity> entities = new List<OrgInsuranceSupplierEntity>(); 

            for (int i = 0; OrgInsuranceExcludes!=null && i < OrgInsuranceExcludes.Count; i++)
            {
                OrgInsuranceSupplierEntity OrgInsuranceSupplierEntity = await _unitOfWork.ModifyClient.Queryable<OrgInsuranceSupplierEntity>().Where(t => t.SupplierId == OrgInsuranceExcludes[i].SupplierId && t.SettingId == OrgInsuranceExcludes[i].SettingId && t.OrgId == OrgInsuranceExcludes[i].OrgId).FirstAsync();

                if (OrgInsuranceSupplierEntity!=null)
                {
                    OrgInsuranceSupplierEntity.IsExcludeSupplier = OrgInsuranceExcludes[i].IsExcludeSupplier;

                    await _unitOfWork.ModifyClient.Updateable<OrgInsuranceSupplierEntity>(OrgInsuranceExcludes).UpdateColumns(t => new { t.IsExcludeSupplier }).WhereColumns(t => new { t.Id }).ExecuteCommandAsync();
                    entities.Add(OrgInsuranceSupplierEntity);
                }
                else
                {
                    bool Isok = await _unitOfWork.ModifyClient.Insertable<OrgInsuranceSupplierEntity>(OrgInsuranceExcludes[i]).ExecuteCommandIdentityIntoEntityAsync();
                    entities.Add(OrgInsuranceExcludes[i]);
                }

            }
            return ResultDto<List<OrgInsuranceSupplierEntity>>.Success("成功", entities);
        }

        public async Task<ResultDto<Boolean>> BatchUpdateOrgInsuranceExcludes(List<OrgInsuranceSupplierEntity> OrgInsuranceExcludes)
        {
            await _unitOfWork.ModifyClient.Updateable<OrgInsuranceSupplierEntity>(OrgInsuranceExcludes).UpdateColumns(t => new {t.IsExcludeSupplier }).WhereColumns(t=> new { t.Id}).ExecuteCommandAsync();
            return ResultDto<Boolean>.Success("成功", true);
        }

        
        public async Task<bool> BatchDelOrgInsuranceExcludes(List<OrgInsuranceSupplierEntity> OrgInsuranceExcludes)
        {
            int result = await _unitOfWork.ModifyClient.Deleteable<OrgInsuranceSupplierEntity>(OrgInsuranceExcludes).ExecuteCommandAsync();

            return false;
        }

        public async Task<List<OrgInsuranceSupplierEntity>> GetAllByOrgId(int orgId, int BindType, int BindId)
        {
            return await _unitOfWork.QueryClient.Queryable<OrgInsuranceSupplierEntity,OrgInsuranceSettingEntity>((e, s)
             => new object[] {JoinType.Inner, e.SettingId==s.Id && s.IsDeleted == false && e.IsDeleted == false}).
                Where((e, s) => s.OrgId == orgId).
                Where((e, s) => s.BindType == BindType).
                Where((e, s) => s.BindId == BindId).ToListAsync();
        }
        public async Task<List<OrgInsuranceSupplierEntity>> GetAllByOrgId(int orgId, int BindType, int BindId,int SupplierId)
        {
            return await _unitOfWork.QueryClient.Queryable<OrgInsuranceSupplierEntity, OrgInsuranceSettingEntity>((e, s)
              => new object[] { JoinType.Inner, e.SettingId == s.Id && s.IsDeleted == false && e.IsDeleted == false }).
                Where((e, s) => s.OrgId == orgId).
                Where((e, s) => s.BindType == BindType).
                Where((e, s) => s.BindId == BindId).
                Where((e, s) => e.SupplierId == SupplierId).ToListAsync();
        }

        public async Task<List<OrgInsuranceSupplierEntity>> GetAllBySettingId(int SettingId) 
        {
            return await _unitOfWork.QueryClient.Queryable<OrgInsuranceSupplierEntity>().Where(t => t.SettingId == SettingId).ToListAsync();
        }
    }
}